<template>
    <a-card>
        <JoblevelForm type="add" @success="getAllJoblevels"></JoblevelForm>
    </a-card>
    <a-card>
        <a-table ref="table" :columns="table.columns" :data="table.data" :loading="table.loading"
                 :row-selection="table.rowSelection" @select="handleSelectRow"
        >
            <template #optional="{record}">
                <a-row :gutter="8">
                    <a-col :span="6">
                        <a-button type="primary" @click="handleOpenEditModal(record)">修改</a-button>
                    </a-col>
                    <a-col :span="6">
                        <a-button type="primary" status="danger" @click="handleOpenDelWarnModal(record)">删除
                        </a-button>
                    </a-col>
                </a-row>
            </template>
        </a-table>
        <a-row>
            <a-col :span="4">
                <a-button @click="handleOpenDelWarnModal" type="primary" status="danger"
                          :disabled="table.moreDeleteBtnDisabled">
                    批量删除
                </a-button>
            </a-col>
        </a-row>
    </a-card>
    <a-modal @cancel="editModal.open=false" :visible="editModal.open" title="修改职称" :footer="false" width="50%">
        <JoblevelForm type="edit" :jobLevel="editModal.data" @success="this.editModal.open=false,getAllJoblevels()"
                      @cancel="editModal.open=false"></JoblevelForm>
    </a-modal>
    <DWM :open="deleteWarnModal.open"
         :name="deleteWarnModal.data.name"
         :rowKeys="deleteWarnModal.rowKeys"
         :tableData="table.data"
         @ok="delJoblevel" @cancel="deleteWarnModal.open=false"></DWM>
</template>

<script>
    import JoblevelForm from './components/JoblevelForm'
    import DWM from '../../../components/common/DelWarnModal'

    export default {
        name: "",
        components: {JoblevelForm, DWM},
        data() {
            return {
                table: {
                    loading: false,
                    rowSelection: {
                        type: 'checkbox',
                        showCheckedAll: false,
                        selectedRowKeys: []
                    },
                    columns: [
                        {
                            title: '编号',
                            dataIndex: 'id',
                        },
                        {
                            title: '职称名称',
                            dataIndex: 'name'
                        },
                        {
                            title: '职称等级',
                            dataIndex: 'titleLevel'
                        },
                        {
                            title: '创建日期',
                            dataIndex: 'createDate'
                        },
                        {
                            title: '启用',
                            dataIndex: 'enabled'
                        },
                        {
                            title: '操作',
                            slotName: 'optional'
                        }
                    ],
                    data: [],
                    moreDeleteBtnDisabled: true
                },
                editModal: {
                    open: false,
                    data: {}
                },
                deleteWarnModal: {
                    open: false,
                    type: 'one',
                    title: '职称',
                    data: {},
                    rowKeys: []
                }
            }
        },
        methods: {
            getAllJoblevels() {
                this.table.loading = true
                this.$get(this.requestUrl.getAllJoblevels.url).then(res => {
                    if (res) {
                        this.table.data = res.obj
                    }
                    this.table.loading = false
                })
            },
            handleOpenEditModal(record) {
                this.editModal.data = record
                this.editModal.open = true
            },
            handleOpenDelWarnModal(record) {
                if (record.id) {
                    this.deleteWarnModal.data = record
                    this.deleteWarnModal.rowKeys = []
                    this.table.rowSelection.selectedRowKeys = []
                    this.table.moreDeleteBtnDisabled = false
                } else {
                    this.deleteWarnModal.data = {name: []}
                }
                this.deleteWarnModal.open = true
            },
            handleSelectRow(rowKeys) {
                this.deleteWarnModal.rowKeys = rowKeys
                this.table.rowSelection.selectedRowKeys = rowKeys
            },
            delJoblevel() {
                let url;
                if (this.deleteWarnModal.data.name instanceof Array) {
                    url = this.requestUrl.deleteMoreJoblevel.url + '?ids=' + this.deleteWarnModal.rowKeys
                } else {
                    url = this.requestUrl.deleteJoblevel.url + this.deleteWarnModal.data.id
                }
                this.$delete(url).then(res => {
                    this.$message.success(res.message)
                    this.deleteWarnModal.open = false
                    this.getAllJoblevels()
                })
            }
        },
        watch: {
            'deleteWarnModal.rowKeys'() {
                if (this.deleteWarnModal.rowKeys.length > 0) {
                    this.table.moreDeleteBtnDisabled = false
                } else {
                    this.table.moreDeleteBtnDisabled = true
                }
            }
        },
        mounted() {
            this.getAllJoblevels()
        }

    }
</script>

<style scoped>

</style>
